package com.czl.module_work.fragment

import android.view.View
import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.StringUtils
import com.blankj.utilcode.util.TimeUtils
import com.blankj.utilcode.util.ToastUtils
import com.czl.base.base.BaseFragment
import com.czl.base.callback.TimePickerListener
import com.czl.base.config.AppConstants
import com.czl.base.util.GlideEngine
import com.czl.base.widget.TimePickerPopup
import com.czl.module_work.BR
import com.czl.module_work.R
import com.czl.module_work.databinding.WorkFrgamentUserInviteBinding
import com.czl.module_work.view.InviteTypePop
import com.czl.module_work.viewModel.UserInviteViewModel
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureConfig
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.listener.OnResultCallbackListener
import com.lxj.xpopup.XPopup
import java.util.*


/**
 * 创建日期：2022/1/5  21:24
 * 类说明:
 * @author：86152
 */
@Route(path = AppConstants.Router.Work.F_WORK_USER_INVITE)
class UserInviteFragment : BaseFragment<WorkFrgamentUserInviteBinding, UserInviteViewModel>() {
    override fun useBaseLayout(): Boolean = false;

    override fun initContentView(): Int = R.layout.work_frgament_user_invite

    override fun initVariableId(): Int = BR.viewModel

    override fun initData() {
        viewModel.tvTitle.set("访客邀约")
        initAdapter()
    }

    private fun initAdapter() {
        viewModel.uc.deleteImgEvent.observe(this, {
            viewModel.guestFaceUrl.set("")
        })
        viewModel.uc.choiceTypeEvent.observe(this, Observer {
            it ?: return@Observer

            val pop = if (it == "0") {
                InviteTypePop(
                    arrayListOf("亲朋好友", "外卖", "家政", "普通访客", "VIP访客", "特殊访客"),
                    requireContext()
                )

            } else {
                InviteTypePop(
                    arrayListOf(
                        "二代身份证", "驾驶证", "港澳通行证", "一代身份证",
                        "军官证", "居住证", "护照", "回乡证", "台胞证"
                    ), requireContext()
                )
            }
            pop.setFilterResultListener { it1 ->
                if (it == "0") {
                    viewModel.inviteTypeName.set(it1)
                } else {
                    viewModel.typeName.set(it1)
                }
            }
            XPopup.Builder(requireContext())
                .asCustom(pop)
                .show()
        })
        viewModel.uc.choiceImgEvent.observe(this, {
            PictureSelector.create(this)
                .openGallery(PictureMimeType.ofImage())
                .imageEngine(GlideEngine.createGlideEngine())
                .isCompress(true)
                .selectionMode(PictureConfig.SINGLE)
                .minimumCompressSize(100)
                .forResult(object : OnResultCallbackListener<LocalMedia?> {
                    override fun onResult(result: List<LocalMedia?>) {
                        result[0]?.let { it -> viewModel.uploadImg(it.compressPath) }
                    }

                    override fun onCancel() {
                        // onCancel Callback
                    }
                })
        })
        viewModel.uc.showTime.observe(this, Observer {
            val popup: TimePickerPopup =
                TimePickerPopup(requireContext()) //.setDefaultDate(date)  //设置默认选中日期
                    //                        .setYearRange(1990, 1999) //设置年份范围
                    //                        .setDateRange(date, date2) //设置日期范围
                    .setTimePickerListener(object : TimePickerListener {
                        override fun onTimeChanged(date: Date?) {
                            //时间改变
                        }

                        override fun onTimeConfirm(date: Date, view: View?) {

                            //点击确认时间
                            if (it == "0") {
                                if (!StringUtils.isEmpty(viewModel.endTime.get())) {
                                    val endTimeLong = TimeUtils.string2Millis(
                                        viewModel.endTime.get(),
                                        "yyyy-MM-dd"
                                    )
                                    val dateLong = TimeUtils.string2Millis(
                                        TimeUtils.date2String(
                                            date,
                                            "yyyy-MM-dd"
                                        ), "yyyy-MM-dd"
                                    )
                                    if (endTimeLong < dateLong) {
                                        ToastUtils.showShort("结束日期不能小于来访日期")
                                        return
                                    }
                                }
                                viewModel.startTime.set(TimeUtils.date2String(date, "yyyy-MM-dd"))
                            } else {
                                if (!StringUtils.isEmpty(viewModel.startTime.get())) {
                                    val startTimeLong = TimeUtils.string2Millis(
                                        viewModel.startTime.get(),
                                        "yyyy-MM-dd"
                                    )
                                    val dateLong = TimeUtils.string2Millis(
                                        TimeUtils.date2String(
                                            date,
                                            "yyyy-MM-dd"
                                        ), "yyyy-MM-dd"
                                    )
                                    if (dateLong < startTimeLong) {
                                        ToastUtils.showShort("结束日期不能小于来访日期")
                                        return
                                    }
                                }
                                viewModel.endTime.set(TimeUtils.date2String(date, "yyyy-MM-dd"))
                            }
                        }
                    })

            XPopup.Builder(requireContext())
                .asCustom(popup)
                .show()
        })
    }
}